package com.android.fragmentdemo
import android.os.Bundle
import com.hengfoxwolf.commonbase.base.BaseActivity2
import com.android.fragmentdemo.databinding.ActivityRxJavaCaseBinding
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.core.Observer
import io.reactivex.rxjava3.disposables.Disposable
import io.reactivex.rxjava3.schedulers.Schedulers
import java.util.concurrent.TimeUnit
import com.hengfoxwolf.commonbase.base.util.LogX

class RxJavaCaseActivity :
    BaseActivity2<ActivityRxJavaCaseBinding>(ActivityRxJavaCaseBinding::inflate) {

    override fun init(savedInstanceState: Bundle?) {
        //testZip()
        //testMerge()
        testFlatMap()
    }

    private fun testZip() {
        val ob1: Observable<Long> = Observable.intervalRange(0, 5, 1, 1, TimeUnit.SECONDS)
        val ob2: Observable<String> = Observable.fromArray("A", "B", "C", "D")
        val ob3: Observable<Int> = Observable.just(1, 2, 3)
        Observable.zip(ob1, ob2, ob3) { t1, t2, t3 -> "result:${t1}${t2}${t3}" }
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                LogX.d(TAG, it)
            }
    }

    private fun testMerge() {
        val ob1 = Observable.fromArray("1", "2", "3")
        val ob2 = Observable.just("a", "b")
        Observable
            .merge(ob1,ob2)
            .subscribe {
                LogX.d(TAG, it)
            }
    }

    private fun testFlatMap() {
        //1.执行标记A处代码块时，if部分只会发射数据一次接收数据一次，else部分则两次，打印Log部分则随前面的flatMap发射数据而定。
        //2.执行标记B处代码块时，会发射数据两次接收数据两次，打印Log部分则随前面的flatMap发射数据而定。
        //3.执行标记C处代码块时，发射数据次数随前面的flatMap发射数据而定。
        //4.标记A处代码块和标记D处代码块同时存在时，标记D处会发射数据三次接收数据三次，标记A处代码不会发射数据，打印Log部分则随前面的flatMap发射数据而定。
        val dataList = mutableListOf(1, 2, 3, 4, 5)
        var count = 0
        Observable.fromIterable(dataList)
            .flatMap {
                LogX.d(TAG,"it:${it}")
                Observable.just("abc:${it}")
            }.flatMap {
                count++
                LogX.d(TAG,"count:${count}")
                //标记A
                return@flatMap if (count == dataList.size) {
                    LogX.d(TAG,"55555")
                    Observable.just(100000)
                } else {
                    LogX.d(TAG,"666666")
                    Observable.just(1)
                }

                //标记B
                //return@flatMap Observable.just(1)

                //标记C
                // Observable.just(600)
            }

            //标记D
            .flatMap {
                Observable.just(300)
            }

            //.doFinally {
            //    LogX.d(TAG, "doFinally")
            //}
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Observer<Int> {
                override fun onSubscribe(disposable: Disposable) {
                }
                override fun onError(ex: Throwable) {
                }
                override fun onNext(result: Int) {
                    LogX.d(TAG, "result:${result}")
                }
                override fun onComplete() {
                    LogX.d(TAG, "onComplete()")
                }
            })
    }

    companion object {
        private const val TAG = "RxJavaCaseActivity"
    }
}